package me.firstapp.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串的帮助类，提供静态方法，不可以实例化。
 * 
 * @Created by keanu.
 * @Copyright (c) 2016, All Rights Reserved.
 * @website http://firstapp.me
 */
public class StrUtils {

	private static Logger logger = LoggerFactory.getLogger(StrUtils.class);
	/**
	 * 禁止实例化
	 */
	private StrUtils() {
	}

	/**
	 * 判断字符串是否为邮件地址
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isMail(String text) {
		// 若为空，返回false
		if (isNULL(text)) {
			return false;
		}
		Pattern p = Pattern
				.compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
		Matcher m = p.matcher(text);
		return m.matches();
	}

	/**
	 * 判断是否为手机号
	 * 
	 * @param mobile
	 */
	public static boolean isMobile(String mobile) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isNULL(String text) {
		if (text == null || text.trim().equals("") || "null".equalsIgnoreCase(text)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isNotNULL(String text) {
		return !isNULL(text);
	}

	/**
	 * 判断昵称的组成只能是字母、汉字、数字、下划线
	 * 
	 * @param name
	 */
	public static boolean isUserName(String name) {
		String str = "^[0-9a-zA-Z_\u3E00-\u9FA5]+$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(name);
		return m.matches();
	}

	/**
	 * 输入流转换字符串
	 * 
	 * @param in
	 */
	public static String getStrFromInputStream(InputStream in) {
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String line = "";
			while ((line = bf.readLine()) != null) {
				buffer.append(line);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}

	public static String getLoginToken() {
		String uuidString = UUID.randomUUID().toString().replaceAll("-", "");
		return uuidString.toUpperCase();
	}

	/**
	 * 获取随机验证码
	 * 
	 * @param length
	 *            验证码长度
	 */
	public static String getVerifyCode(int length) {
		StringBuffer buffer = new StringBuffer("0123456789");
		int range = buffer.length();
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}

	public static String captureJavascript(String strURL) throws Exception {
		URL url = new URL(strURL);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		InputStreamReader input = new InputStreamReader(httpConn.getInputStream(), "utf-8");
		BufferedReader bufReader = new BufferedReader(input);
		String line = "";
		StringBuilder contentBuf = new StringBuilder();
		while ((line = bufReader.readLine()) != null) {
			contentBuf.append(line);
		}
		return contentBuf.toString();
	}

	public static int getRandomInt() {
		return (int) (1 + Math.random() * (9 - 1 + 1));
	}

	public static void main(String[] args) {
		// System.out.println(getVerifyCode(6));
		// BigDecimal a = new BigDecimal(9.5);
		// System.err.println(a.intValue());
		// System.err.println(a.intValueExact());
		// String info1 = "朱尔双肩背包女2017新款韩版书包百搭女士时尚包包休闲女包双肩包【包邮】";
		// String info2 = "【在售价】339.00元";
		// String info3 = "【券后价】239.00元";
		// String info4 = "【下单链接】http://e22a.com/h.rk4bio";
		// String info5 = "-----------------";
		// String info6 = "复制这条信息，￥SdnL03tZK9l￥ ，打开【手机淘宝】即可查看";
		// String info = info1+info2+info3+info4+info5+info6;
		// System.out.println(info);
		// int a = info.indexOf("【在售价】");
		// System.out.println(a);
		// int b = info.indexOf("元");
		// System.out.println(b);
		//
		// System.out.println("标题："+info.substring(0, a));
		// System.out.println("在售价:"+info.substring(a+5, b));
		//
		// int c = info.indexOf("【券后价】");
		// int num = c;
		// for (int i = c; i < info.length(); i++) {
		// num = num + 1;
		// if(info.charAt(num) == '元'){
		// System.out.println(num+"-"+info.charAt(num));
		// break;
		// }
		// }
		// System.out.println("券后价:"+info.substring(c+5, num));
		// int d = info.indexOf("【下单链接】");
		// int e = info.indexOf("--------");
		// System.out.println("下单链接:"+info.substring(d+6, e));
		// String url = info.substring(d+6, e);
		// String copy = info.substring(e+8, info.length());
		// String copyStr = new String();
		// for (int i = 0; i < copy.length(); i++) {
		// char heng = copy.charAt(i);
		// if('-' == heng){
		// copyStr = copy.substring(i+1, copy.length());
		// }
		//
		// }
		// System.out.println("复制信息："+copyStr);
		//
		// try {
		// String javascript = captureJavascript(url);
		// System.out.println(javascript);
		// int f = javascript.indexOf("var extraData");
		// int g = javascript.indexOf("};");
		// String usefulJavascript = javascript.substring(f, g+1);
		// System.out.println(usefulJavascript);
		// int h = usefulJavascript.indexOf("{");
		// int i = usefulJavascript.indexOf("}");
		// String extraData = usefulJavascript.substring(h, i+1);
		// System.out.println(extraData);
		// } catch (Exception e1) {
		// e1.printStackTrace();
		// }
		// 淘宝URL：https://item.taobao.com/item.htm?id=
		logger.debug(MD5Utils.generate("199.2352"));
		// StringBuffer str = new
		// StringBuffer(Calendar.getInstance().getTimeInMillis()+"");
		// System.out.println(str.toString());
		// System.out.println(str.reverse().toString());

	}
}
